package com.gmrz.uap.model;

import com.gmrz.uap.cfg.SystemConst;
import com.gmrz.uap.model.base.BaseUser;
import com.gmrz.uap.util.DateUtil;
import com.jfinal.kit.Kv;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.SqlPara;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;

import java.sql.Timestamp;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class User extends BaseUser<User> {
    public static final User dao = new User();

    private static final String NAMESPACE = "user";
    private static final String SQL_SELECT_COMM = NAMESPACE + SystemConst._DEFAULT_SEPARATOR_DOT + "select_comm";
    private static final String SQL_SELECT_FIND_ALL = NAMESPACE + SystemConst._DEFAULT_SEPARATOR_DOT + "findAll";
    private static final String SQL_SELECT_USERNAME_PASSWORD = NAMESPACE + SystemConst._DEFAULT_SEPARATOR_DOT + "getUserByUsernameAndPassword";
    private static final String SQL_SELECT_USERNAME = NAMESPACE + SystemConst._DEFAULT_SEPARATOR_DOT + "getUserByUsername";
    private static final String SQL_UPDATE_IDS_PWD = NAMESPACE + SystemConst._DEFAULT_SEPARATOR_DOT + "updatePassword";


    /**
     * 根据页面传过来的条件进行分页查询
     *
     * @param user  管理员信息
     * @param page  页号
     * @param rows  行数
     * @param sort  排序字段
     * @param order 升序/降序
     * @return　分页结果 Page<User>
     */
    public List<User> findListForPaginate(User user, int page, int rows, String sort, String order) {

        StringBuilder sql = new StringBuilder();

        sql.append("from t_user where user_id=user_id ");

        Iterator i = user._getAttrsEntrySet().iterator();

        //循环遍历传过来的参数，并将参数拼接到SQL
        while (i.hasNext()) {
            Map.Entry<String, Object> obj = (Map.Entry<String, Object>) i.next();

            if (StrKit.isBlank(obj.getKey()))
                continue;

            if (StringUtils.equalsIgnoreCase("user_id", obj.getKey())) {
                sql.append(" and " + obj.getKey() + " = " + obj.getValue());
                continue;
            }

            //如果数据为空或为NULL的时候，跳过此次循环进入下一次循环
            if (null == obj.getValue() || "" == obj.getValue()) {
                continue;
            }
            sql.append(" and " + obj.getKey() + " like '%" + obj.getValue() + "%'");

        }

        sql.append(" order by " + sort + " " + order);

        //return this.paginate(page, rows, getSql(SQL_SELECT_COMM), sql.toString());
        String sqlStr = getSql(SQL_SELECT_COMM) + sql.toString();
        return this.find(sqlStr);
    }

    /**
     * 根据用户名获取管理员信息
     *
     * @param username 用户名
     * @return User 管理员信息
     */
    public User getUserByUsername(String username) {
        return this.findFirst(getSql(SQL_SELECT_USERNAME), username);
    }

    /**
     * 根据用户名和密码获得用户信息
     *
     * @param username 用户名
     * @param password 密码
     * @return 管理员信息
     */
    public User getUserByUsernameAndPassword(String username, String password) {
        return this.findFirst(getSql(SQL_SELECT_USERNAME_PASSWORD), username, password);
    }

    /**
     * 获取所有用户信息
     *
     * @return 用户信息集合
     */
    public List<User> findAll() {
        return this.find(getSql(SQL_SELECT_FIND_ALL));
    }


    public boolean resetPassword(String[] ids, String pwd) {
        DateTime before = new DateTime().minusDays(91);
        Kv cond = Kv.by("ids", ids).set("pwd", pwd).set("updatets", DateUtil.dateToStr(new Timestamp(before.getMillis()), DateUtil.simple));
        SqlPara sqlPara = Db.getSqlPara(SQL_UPDATE_IDS_PWD, cond);
        return Db.update(sqlPara.getSql()) > 0;
    }

    public boolean updatePassword(String[] ids, String pwd) {
        Kv cond = Kv.by("ids", ids).set("pwd", pwd).set("updatets", DateUtil.dateToStr(new Date(), DateUtil.simple));
        SqlPara sqlPara = Db.getSqlPara(SQL_UPDATE_IDS_PWD, cond);
        return Db.update(sqlPara.getSql()) > 0;
    }


    @Override
    public String toString() {
        return this.getUserName();
    }


}
